import inspect
from common.base.base_api import BaseApi

from settings import settings
from services.server_integration.schemas.alarms_request import AlarmsRequest
from services.server_integration.schemas.cities_timezone import TimezonesRequest
from services.new_services.new_server_integration.schemas.time_gps import GpsTime
from services.new_services.new_server_integration.schemas.block_info import BlockInfoRequest
from services.new_services.new_server_integration.schemas.alarms_request import NewAlarmsRequest
from services.new_services.new_server_integration.schemas.alarm_configuration import DeleteAlarms
from services.new_services.new_server_integration.schemas.response_message import ResponseMessage
from services.new_services.new_server_integration.schemas.short_block_info import ShortBlockInfoResponse
from services.new_services.new_server_integration.schemas.enable_configuration import LinksConnectionType
from services.new_services.new_server_integration.schemas.cviya_configuration import AvailableConnectionRequest
from services.new_services.new_server_integration.schemas.full_unit_configuration import NewFullUnitConfiguration
from services.new_services.new_server_integration.schemas.change_block_mode_configuration import ChangeBlockModeRequest


class NewServerIntegrationApi(BaseApi):
    __slots__ = []

    def __init__(self):
        super(NewServerIntegrationApi, self).__init__(request_queue_uuid=settings.NEW_SERVER_INTEGRATION_ROUTER_REQUEST_QUEUE)

    async def get_gps_time(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def set_gps_time(self, gps_time: GpsTime):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'gps_time': gps_time
            },
            timeout=11 * 60
        )

    async def delete_alarms(self, alarms_to_be_deleted: DeleteAlarms) -> ResponseMessage:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'alarms_to_be_deleted': alarms_to_be_deleted
            },
            timeout=11 * 60
        )

    async def upload_configuration(self, config: NewFullUnitConfiguration, user_role: str = 'user') -> ResponseMessage:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'config': config,
                'user_role': user_role
            },
            timeout=11 * 60
        )

    async def get_cities_timezones(self, request: TimezonesRequest):
        res = await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'request': request})
        return res

    async def change_block_mode(self, config: ChangeBlockModeRequest):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'config': config
            },
            timeout=11 * 60
        )

    async def enable_radio_or_optics(self, config: LinksConnectionType, user_role: str):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {
            'config': config,
            'user_role': user_role
        })

    async def get_log_alarms(self, request: NewAlarmsRequest, user_role: str):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {
            'request': request,
            'user_role': user_role
        })

    async def get_current_alarms(self, request: NewAlarmsRequest, user_role: str):
        alarms = await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {
            'request': request,
            'user_role': user_role
        })

        return alarms

    async def get_subblocks_statuses(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def get_telemetry(self, user_role):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {'user_role': user_role})

    async def get_available_connection(self, config: AvailableConnectionRequest):
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'config': config
            },
            timeout=11 * 60
        )

    async def get_block_info(self, config: BlockInfoRequest) -> ShortBlockInfoResponse:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'config': config
            },
            timeout=11 * 60
        )

    async def get_total_log_alarms(self, request: AlarmsRequest, user_role: str):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {
            'request': request,
            'user_role': user_role
        })

    async def get_current_total_alarms(self, request: AlarmsRequest, user_role: str):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name, {
            'request': request,
            'user_role': user_role
        })

    async def get_block_version(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)

    async def set_new_full_configuration(self, config: NewFullUnitConfiguration, user_role: str = 'user') -> ResponseMessage:
        return await self.send_rpc_request_message(
            inspect.currentframe().f_code.co_name,
            {
                'config': config,
                'user_role': user_role
            },
            timeout=11 * 60
        )

    async def get_new_full_configuration(self):
        return await self.send_rpc_request_message(inspect.currentframe().f_code.co_name)
